from logging import exception
from selenium import webdriver
import time
 
url_list = [
    ("https://learn.kaikeba.com/video/208801", 3*3600+15*60) # 第一课 3:11
    ,("https://learn.kaikeba.com/video/209542", 3600*3) # 第二课 2:54
    ,("https://learn.kaikeba.com/video/211152", 3600*3) # 第三课 2:56
    ,("https://learn.kaikeba.com/video/212079", 3600*2 + 10*60) # 第四课 2:06
    ,("https://learn.kaikeba.com/video/211152", 2*3600+55*60) # 第五课 2:49
]
# 声明浏览器
browser = webdriver.Chrome()
for url, duration in url_list:
    try:
        # get打开指定的url，传入要打开的url,以淘宝网为例
        print('****** url: ', url, "start time: ",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), " *******") 
        browser.get(url)
        
        time.sleep(8)

        # # 通过find_element_by_name获取到网页标签，send_keys()输入内容,在搜索栏输入python
        # browser.find_element_by_name('q').send_keys('python')
        # obj = browser.find_element_by_class_name("vjs-icon-placeholder")
        sleepFlag = True
        while(sleepFlag):
            try:
                obj = browser.find_element_by_class_name("vjs-big-play-button")
                obj.click()
                sleepFlag= False
            except Exception as e:
                print(e)
                time.sleep(10)
            
        print("准备录制：", url, "时长为：", duration)
        # browser.find_element_by_class_name("vjs-big-play-button").click()
        time.sleep(duration)
        # '''
        # 通过find_element_by_class_name获取到网页标签，click()点击
        # 注意class名中的空格是表示父子级的关系，如果有空格，则class_name只取最后一段，然后还要避免有同名的class，不要取错了
        # 为了避免取错标签，优先根据id或name等其他方式取
        # 如：淘宝网的搜索按钮class name为btn-search tb-bg,用btn-search tb-bg取时报错,用tb-bg取则成功
        # '''
        # # browser.find_element_by_class_name('btn-search tb-bg').click()  # 报错
        # browser.find_element_by_class_name('tb-bg').click()  # 成功
        # time.sleep(5)
        # # 点击"密码登录"按钮
        # # browser.find_element_by_class_name('forget-pwd J_Quick2Static').click()  # 报错
        # browser.find_element_by_class_name('J_Quick2Static').click()  # 正常
        # time.sleep(10)
        # browser.close()
        print('url: ', url, "end time: ",time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 
    except Exception as e:
        print("模拟登录失败：{}".format(e))

browser.close()